﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace DotNetUI.Models
{
    /// <summary>
    /// 委托命令
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DelegateCommand<T>: ICommand
    {
        private readonly Action<T> onExeceute;

        private readonly Func<T, bool> canExecute;

        public DelegateCommand(Action<T> onExeceute, Func<T, bool> canExecute)
        {
            this.onExeceute = onExeceute;
            this.canExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            return canExecute?.Invoke((T)parameter) ?? true;
        }

        public void Execute(object parameter)
        {
            onExeceute?.Invoke((T)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }
    /// <summary>
    /// 委托命令
    /// </summary>
    public class DelegateCommand : ICommand
    {
        private readonly Func<object, bool> canExecute;
        private readonly Action<object> onExeceute;

        public DelegateCommand(Action<object> onExeceute, Func<object, bool> canExecute = null)
        {
            this.canExecute = canExecute;
            this.onExeceute = onExeceute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecute?.Invoke(parameter) ?? true;
        }

        public void Execute(object parameter)
        {
            onExeceute?.Invoke(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }
}
